package com.tanhua.sso.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tanhua.sso.entity.User;
import com.tanhua.sso.entity.UserInfo;
import com.tanhua.sso.enums.SexEnum;
import com.tanhua.sso.mapper.UserInfoMapper;
import com.tanhua.sso.mapper.UserMapper;
import com.tanhua.sso.vo.PicUploadResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @Description AUTO
 * @ClassName UserInfoService
 * @Author ZhengXinjie
 * @date 2021.02.10 13:27
 */
@Service
public class UserInfoService {
    private final UserService userService;
    private final UserInfoMapper userInfoMapper;
    private final FaceEngineService faceEngineService;
    private final PicUploadService picUploadService;

    @Autowired
    public UserInfoService(UserService userService, UserInfoMapper userInfoMapper, FaceEngineService faceEngineService, PicUploadService picUploadService) {
        this.userService = userService;
        this.userInfoMapper = userInfoMapper;
        this.faceEngineService = faceEngineService;
        this.picUploadService = picUploadService;
    }

    /**
     * 存储用户的基本信息
     * @param param 请求体参数
     * @param token 登录生成的token
     * @return 是否添加成功
     */
    public boolean saveUserInfo(Map<String, String> param, String token){
        // 校验token
        User user = userService.queryUserByToken(token);
        if(user == null){
            return false;
        }

        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(user.getId());
        userInfo.setSex(StringUtils.equalsIgnoreCase(param.get("gender"), "man") ? SexEnum.MAN : SexEnum.WOMAN);
        userInfo.setNickName(param.get("nickname"));
        userInfo.setBirthday(param.get("birthday"));
        userInfo.setCity(param.get("city"));

        // 如果添加成功则返回true
        return userInfoMapper.insert(userInfo) == 1;
    }

    /**
     * 存储用户头像
     * @param file 上传的图片
     * @param token 用户登录的token
     * @return 是否保存成功
     */
    public boolean saveUserLogo(MultipartFile file, String token){
        // 校验token
        User user = userService.queryUserByToken(token);
        if(user == null){
            return false;
        }

        // 检验是否是人像
        try {
            boolean bool = faceEngineService.checkIsPortrait(file.getBytes());
            // 不是人像返回false
            if(!bool){
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 图片上传到阿里云
        PicUploadResult result = picUploadService.upload(file);
        if(StringUtils.equals("error", result.getStatus())){
            // 上传失败
            return false;
        }

        // 将头像保存到用户信息中
        UserInfo userInfo = new UserInfo();
        userInfo.setLogo(result.getName());


//        UpdateWrapper<UserInfo> updateWrapper = new UpdateWrapper<>();
//        updateWrapper.eq("user_id", user.getId());
//        return userInfoMapper.update(userInfo, updateWrapper) == 1;
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", user.getId());
        return userInfoMapper.update(userInfo, queryWrapper) == 1;
    }
}

